using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using Habanero.Base.Exceptions;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestCreateUIViewFromDomainModel
    {
        #region Setup/Teardown

        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [TearDown]
        public virtual void TearDownTest(){}
        #endregion

        [Test]
        public void Test_CreateField_WhenPropNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            UIViewCreator uiViewCreator = new UIViewCreator();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                uiViewCreator.CreateUIView(null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dmClass", ex.ParamName);
            }
        }

        [Test]
        public void Test_CreateField_WhenClassSolutionNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            dmClass.ClassNameBO = TestUtilsShared.GetRandomString();
            UIViewCreator uiViewCreator = new UIViewCreator();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmClass.Solution);
            //---------------Execute Test ----------------------
            try
            {
                uiViewCreator.CreateUIView(dmClass);
                Assert.Fail("expected HabaneroArgumentExceptifsdon");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("The argument 'dmClass.Solution' is not valid. You cannot " 
                        + "create a UIView for a DMClass that is not associated with a Solution - (DMClass ", ex.Message);
            }
        }

        [Test]
        public void Test_CreateField_WhenString_ShouldHaveTextBoxPropertyAndControlType()
        {
            //---------------Set up test pack-------------------
            SolutionCreator creator = new SolutionCreator();
            DMProperty property = TestUtilsDMProperty.CreateUnsavedDefaultDMPropertyWithClass(creator.CreateSolution());
            property.OrdinalPosition = 3;
            TestUtilsDMProperty.SetDMPropStringType(property);
            DMClass dmClass = (DMClass) property.Class;
            TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass, false);
            TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass, false);
            UIViewCreator uiViewCreator = new UIViewCreator();
            //---------------Assert Precondition----------------
            UICreatorTestUtils.AssertPropertyTypeIsString(property);
            Assert.IsNotNull(property.OrdinalPosition);
            Assert.Greater(property.OrdinalPosition, 0);
            Assert.AreEqual(0, dmClass.UIViews.Count);
            Assert.AreEqual(3, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            UIView uiView = uiViewCreator.CreateUIView(dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(uiView);
            Assert.IsNotNull(uiView.Class);
            Assert.AreSame(dmClass, uiView.Class);
            Assert.AreEqual(1, dmClass.UIViews.Count);

            //----------------Verify BOEditor Created Correctly---------
            UIFormInfo formInfo = uiView.UIFormInfo;
            Assert.IsNotNull(formInfo);
            Assert.AreEqual(3, formInfo.UIFields.Count);

            //---------------Verify GridFilter Created Correctly-------
            UIGridFilter gridFilter = uiView.UIGridInfo.UIGridFilter;
            Assert.IsNotNull(gridFilter);
            Assert.AreEqual(3, gridFilter.UIGridFilterProps.Count);

            //---------------Verify GridInfo Created Correctly---------
            UIGridInfo gridInfo = uiView.UIGridInfo;
            Assert.IsNotNull(gridInfo);
            Assert.AreEqual(3, gridInfo.UIColumns.Count);

        }
    }
}